<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

function otx_admin_header()
{
	$controller_file = otx_param_string('_controller_file');
	if (pathinfo($controller_file, PATHINFO_EXTENSION) == 'inc') {
		return <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="static/css/admin.css" rel="stylesheet" type="text/css" />
<script src="static/js/jquery.js" type="text/javascript"></script>
<script src="static/js/jquery.tabs.js" type="text/javascript"></script>
<script src="static/js/admin.js" type="text/javascript"></script>
</head>
<body>

<div class="content-wrapper">
EOT;
	} else {
		$dirs[] = otx_module_path('admin', 'templates/');
		return otx_template_parse('admin_header', null, $dirs);
	}
}

function otx_admin_footer()
{
	$controller_file = otx_param_string('_controller_file');
	if (pathinfo($controller_file, PATHINFO_EXTENSION) == 'inc') {
		return <<<EOT
</div> <!-- /.content-wrapper -->

</body>
</html>
EOT;
	} else {
		$dirs[] = otx_module_path('admin', 'templates/');
		return otx_template_parse('admin_footer', null, $dirs);
	}
}

function otx_admin_warning($message, $links = array())
{
	otx_admin_message($message, $links, 1);
}

function otx_admin_message($text, $links = array(), $type = 0, $seconds = 3)
{
	otx_param_set('message_text', $text);
	otx_param_set('message_type', $type);
	otx_param_set('message_links', $links);
	otx_param_set('message_seconds', $seconds);
	otx_param_set('action', 'index');
	otx_router_execute('admin/message');

	echo otx_param_string('_content');
	exit;
}

function otx_admin_flash_message($message, $type = 'success')
{
	if (empty($_SESSION['_XFLASH'])) {
		$_SESSION['_XFLASH'] = array();
	}

	$_SESSION['_XFLASH'][] = array(
			'text' => $message,
			'type' => $type
	);
}

function otx_admin_flash_warning($message)
{
	otx_admin_flash_message($message, 'warning');
}

function otx_admin_flash_attention($message)
{
	otx_admin_flash_message($message, 'attention');
}

function otx_admin_flash_error($message)
{
	otx_admin_flash_message($message, 'attention');
}

function otx_admin_critical_message($message)
{
	otx_param_set('message_text', $message);
	otx_router_execute('admin', 'critical', 'index');
	otx_html_response_output();
	exit;
}

function otx_admin_critical_type($type)
{
	otx_param_set('message_type', $type);
	otx_router_execute('admin', 'critical', 'index');
	otx_html_response_output();
	exit;
}

function otx_admin_log_action($text)
{
	$vars['log_time'] = $GLOBALS['_XTIMESTAMP'];
	$vars['log_text'] = $text;
	$vars['log_ip'] = otx_request_client_ip();
	$vars['user_id'] = otx_session_user_id() ? otx_session_user_id() : 0;
	otx_db_insert('#system_log#', $vars);
}

function otx_admin_breadcrumbs_add()
{
	//
}

function otx_admin_breadcrumbs_list()
{
	//
}